from BaseControl import BaseWebControl
from Autotests.LocalMachine.UiObjectsMap.AirClient import BackupRestoreProgress
from Label import WebLabel


class WebProgressBar(BaseWebControl):
    def __init__(self, handler):
        super(WebProgressBar, self).__init__(handler)

    def performWait(self, completeTimeout, startTimeout = 10, message=''):
        self.waitForStart(startTimeout, "%s on start waiting" % message)
        self.waitForComplete(completeTimeout, "%s on complete waiting" % message)

    def waitForComplete(self, timeout, message='progress bar complete timeout'):
        self.waitFor(lambda x: self.isComplete(), timeout, message)

    def waitForStart(self, timeout, message='progress bar start timeout'):
        self.waitFor(lambda x: not self.isComplete(), timeout, message)

    def isComplete(self):
        pass

    def percent(self):
        pass


class BackupRestoreProgressBar(WebProgressBar):
    def __init__(self):
        super(BackupRestoreProgressBar, self).__init__(BackupRestoreProgress.progressBar)
        self._completeMarker = WebLabel(BackupRestoreProgress.backupCompleteMarker)
        self._percentLabel = WebLabel(BackupRestoreProgress.percentLabel)

    def isComplete(self):
        return not self._completeMarker.isExists()

    def percent(self):
        percent = 100
        if not self.isComplete():
            percent = self._percentLabel.text().split()[0].strip('%')
        return percent
